// Loading层相关 the same as $(document).ready(function(){}); 
$().ready(function() {
  // 创建Loading层 wating image
	var divLoading = $("<div id='loading'><img src='/images/wait.gif'/>Loading...</div>");
	$("body").append(divLoading);
	$("#loading").attr("style", "display:none;width:80px;padding:5px 5px 5px 8px;border:1px solid #eea60e;background-color:#fdf4e1;")
	
	//when ajax starting,do this function
	$("#loading").ajaxStart(function() {
	  $(this).show();
	  $(this).css("position", "absolute");
	  $(this).css("top", document.documentElement.scrollTop + 8);
	  $(this).css("left", window.document.documentElement.clientWidth - 120);
	});
	
	// 结束ajax请求时隐藏Loading when ajax stoping,do this function
	$("#loading").ajaxStop(function() {
	  $(this).hide();
	  // 挡住select
	  var obj = $("#div").find("iframe")[0];
	  if (obj != null) {
	    obj.style.height = ($("#div").height()+10) + "px";
	  }
	});
	
	// 滚动时保持在右上角
	$(window).scroll(function() { 
	  $("#loading").css("top", document.documentElement.scrollTop + 8);
	});
	
	// 缩放窗口时保持在右上角
	$(window).resize(function() {
	  $("#loading").css("left", window.document.documentElement.clientWidth - 160);
	});
});

// ajax时获得FORM中的值的组合
function getFormQueryString(frmID) {
  var i, queryString = "", and = "";
  var item; // for each form's object
  var itemValue; // store each form object's value

  for (i = 0; i < frmID.length; i++) {
    item = frmID[i]; // get form's each object

    if (item.name != '') {
      if (item.type == 'select-one') {
        itemValue = item.options[item.selectedIndex].value;
      }
      else if (item.type == 'checkbox' || item.type == 'radio') {
        if (item.checked == false) {
          continue;
        }
        itemValue = item.value;
      }
      else if (item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image') {
        continue;
      }
      else {
        itemValue = item.value;
      }

      itemValue = encodeURIComponent(itemValue);
      queryString += and + item.name + '=' + itemValue;
      and = "&";
    }
  }
  return queryString;
}

function getSession(httpRequest) {
	$.ajax({
      type: "post", 
      url: "/main/getsession.jsp", 
      data: "r=" + Math.random(),
      timeout: 5000,
      success: function(msg) {
        msg = $.trim(msg);
        if (msg != "") {//so login timeout
            httpRequest.abort();
            self.location.href = '/main/sessionend.jsp';//aim to login page
        }
      },
      error: function(msg, thrown) {
        httpRequest.abort();
        //if (top.frames)
        //{
        //    top.frames['topframe'].document.getElementById('question').value = '1';
        //}
        if (window.navigator.browserLanguage == 'zh-cn') {
            alert('服务器没有响应，请检查连接！');
        } else {
            alert('Server has no response, please check connection!');
        }
        //top.location.href = 'about:blank';
      }
    });
}

function setDataHover(selector) {
	var dataName = '#list > table[class="tbl"] tr';
    if ((selector != null) && (selector != "undefined") && (selector != '')) {
        dataName = selector;
    }
	$(dataName).hover(
        function () {$(this).addClass('table_tr_hover');},
        function () {$(this).removeClass('table_tr_hover');}
    );
}